package com.youlian.cloud.common.config;

import com.alibaba.arms.filter.EagleEyeFilter;
import com.youlian.cloud.core.config.MvcCommonConfiguration;
import com.youlian.cloud.log.asepct.TimeInterceptor;
import com.youlian.cloud.log.filter.CatHttpRequestFilter;
import com.youlian.cloud.log.filter.HttpCatCrossFliter;
import feign.Feign;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuhx
 * @create 2019/08/06 10:29
 * @email liuhx@elab-plus.com
 **/
public class CommonConfig extends MvcCommonConfiguration {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getTimeHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }


    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", addcorsConfig());
        return new CorsFilter(source);
    }

    private CorsConfiguration addcorsConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        List<String> list = new ArrayList<>();
        list.add("*");
        corsConfiguration.setAllowedOrigins(list);
    /*
    // 请求常用的三种配置，*代表允许所有，当时你也可以自定义属性（比如header只能带什么，只能是post方式等等）
    */
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

    /**
     * cat 路径拦截器
     *
     * @return
     */
    public HandlerInterceptor getTimeHandlerInterceptor() {
        return new TimeInterceptor();
    }

    @Bean
    public FilterRegistrationBean catFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        HttpCatCrossFliter filter = new HttpCatCrossFliter();
        registration.setFilter(filter);
        registration.addUrlPatterns("/*");
        registration.setName("cat-filter");
        registration.setOrder(2);
        return registration;
    }

    /**
     * 请求日志埋点
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean catRequestFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        CatHttpRequestFilter filter = new CatHttpRequestFilter();
        registration.setFilter(filter);
        registration.addUrlPatterns("/*");
        registration.setName("cat-request-filter");
        registration.setOrder(3);
        return registration;
    }


    @Bean
    public FilterRegistrationBean EagleEyeFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        EagleEyeFilter filter = new EagleEyeFilter();
        registration.setFilter(filter);
        registration.addUrlPatterns("/*");
        registration.setName("eagleEyeFilter-filter");
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public Feign.Builder feign() {
        return Feign.builder().logLevel(feign.Logger.Level.BASIC);
    }


}
